#include<arpa/inet.h>
#include<stdlib.h>
#include<iostream>
#include<stdio.h>

#define SIZE 100

using namespace  std;

void close_connection( int client_socket) {
		shutdown(client_socket, SHUT_RDWR);
		close(client_socket);
}


void talk_with_server(int socket, sockaddr_in* client) {
	
}



int main(int argc, char** argv) {
	cout << "Echo client" << endl;
	in_addr host;
	inet_aton(argv[1], &host);
	int sckt = socket(AF_INET, SOCK_DGRAM, 0);
	sockaddr_in remote_server;
	remote_server.sin_family = AF_INET;
	remote_server.sin_addr.s_addr = host.s_addr; //(errcheck = inet_addr(host_name));
	remote_server.sin_port = htons(atoi(argv[2]));
	
	socklen_t struct_size = sizeof(remote_server);
	char buffer[SIZE];
	while(true) {	
		cin.getline(buffer, SIZE);
		int size = 0;
		size = sendto(sckt, buffer, SIZE, 0, (sockaddr *)&remote_server, struct_size);
		if(size <= 0) {
			if(size < 0)
				perror("Send: ");
		} else {	
			size = recvfrom(sckt, buffer, size, 0, (sockaddr *)&remote_server, &struct_size);
			if(size < 0) {
				perror("Receive: ");
			}
			cout << "From server:"<<buffer << endl;
		}
	}
	return 0;
}
